﻿/**

 @Name：layuiAdmin 用户管理，增删改查
 @Author：马达
 @Site：http://www.sunplus.wang
 @License: 杭州九宸智能科技有限公司
    
 */

layui.define(['table', 'form', 'laydate', 'upload', 'admin'], function (exports) {
    var $ = layui.$
    , admin = layui.admin
    , view = layui.view
    , table = layui.table
    , upload = layui.upload
    , laydate = layui.laydate
    , form = layui.form;

    var varButtonRight = "";
    //文章管理
    table.render({
        elem: '#LAY-app-content-list'
        , toolbar: true  //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , defaultToolbar: ['filter']
        , method: "post"
        , url: layui.setter.APIUrl + '/shop/order_returnMgr/listPain'
        , headers: { //通过 request 头传递
            Authorization: "bearer " + layui.data('layuiAdmin').access_token
        }
        , parseData: function (res) { //res 即为原始返回的数据 
            varButtonRight = res.Data.ButtonRight;
            return {
                "ResultCode": res.ResultCode, //解析接口状态
                "ResultInfo": res.ResultInfo, //解析提示文本
                "count": res.Data.TotalRecordCount, //解析数据长度
                "Data": res.Data.Items //解析数据列表 
            };
        }
    , cols: [[
      { type: 'checkbox', fixed: 'left', field: 'PID' }, //sort: true  //hide: true //  width:100
		{
		    field: 'Code', title: '退款单号<br/>订单号', minWidth: 150, templet: function (d) {
		        if (d.Title == null) d.Title = "";
		        var html = "<span>" + d.Code + "</span>";
		        html += "<br /><span>" + d.OrderNumber + "</span>";
		        return html;
		    }
		},
		{
		    field: 'ReturnType', title: '退款类型', width: 100, templet: function (d) {
		        var html = "";
		        if (d.ReturnType == 0) {
		            html = "<span class='bksp'>商品退款</span>";
		        } else {
		            html = "<span class='bksp1'>整单退款</span>";
		        }
		        return html;
		    }
		},                       
		{
		    field: 'Name', title: '申请商品<br/>申请数量', minWidth: 180, templet: function (d) {
		        var html = "";
		        if (d.ReturnType == 0) {
		            html = "<span style='font-weight:bold'>" + d.Name + "</span><br />";
		            html += "<span style='font-weight:bold;font-size:1.1em;float:right'>x " + d.Num + "</span>";
		        } else {
		            html = "<span style='color:#c2c2c2'>订单商品</span>";
		        }
		        return html;
		    }
		},                                     
		{
		    field: 'ReturnPrice', title: '退款金额', width: 90, templet: function (d) {
		        var html = "<span style='color:#FF5722;font-weight:bold;'>￥</span><span style='color:#FF5722;font-weight:bold;font-size:1.1em'>" + d.ReturnPrice + "</span>";
		        return html;
		    }
		},
		{
		    field: 'ReturnMode', title: '退货方式', width: 100, templet: function (d) {
		        var html = "";
		        if (d.ReturnMode == 1) {
		            html = "<span style='font-weight:bold'>快递寄回</span>";
		        } else if (d.ReturnMode == 2) {
		            html = "<span style='font-weight:bold'>送回店铺</span>";
		        } else {
		            html = "<span style='color:#d2d2d2;font-weight:bold'>无需退货</span>";
		        }
		        return html;
		    }
		},
		{ field: 'Reason', title: '申请原因', minWidth: 100 },
		{
		    field: 'UserID', title: '申请人/时间', minWidth: 175, templet: function (d) {
		        var html = "<span style='font-weight:bold;'>" + d.MemberCode + "</span> &nbsp;<span>" + d.ApplyUserName + "</span>";
		        html += "<br /><span>" + d.ApplyTime + "</span>";
		        return html;
		    }
		},
		{
		    field: 'UserID', title: '联系人/电话', minWidth: 100, templet: function (d) {
		        var html = "<span>" + d.UserName + "</span>";
		        html += "<br /><span>" + d.UserTel + "</span>";
		        return html;
		    }
		},
		{
		    field: 'State', title: '状态', minWidth: 80, templet: function (d) {
		        var html = "";
		        if (d.State == 0) {
		            html = "<span style='color:#FF5722;font-weight:bold'>申请中</span>";
		        } else if (d.State == 1) {
		            html = "<span style='color:#5FB878;font-weight:bold'>同意退款</span>";
		        } else if (d.State == 2) {
		            html = "<span style='color:#2F4056;font-weight:bold'>拒绝退款</span>";
		        } else {
		            html = "<span style='color:#c2c2c2;font-weight:bold'>取消</span>";
		        }
		        if (d.State == 1 && d.RefundResult == 2) {
		            html += "<br /><span style='color:red;font-weight:bold'>退款失败</span><i class=\"layui-icon tespn\" title='退款失败，点击重新申请' lay-event=\"retry\">&#xe60b;</i>";
		        }
		        return html;
		    }
		},
		{ field: 'StoreName', title: '店铺'},                         
      {
          title: '操作', minWidth: 125, fixed: 'right', align: 'center', templet: function (d) {
              var strRtn = "";
              if (varButtonRight.indexOf('view') >= 0) {
                  strRtn += "<a class=\"layui-btn layui-btn-primary layui-btn-xs\" lay-event=\"view\">查看</a> ";
              }
              if (varButtonRight.indexOf('edit') >= 0) {
                  strRtn += "<a class=\"layui-btn layui-btn-normal layui-btn-xs\" lay-event=\"edit\">编辑</a> ";
              }
              if (varButtonRight.indexOf('del') >= 0) {
                  strRtn += "<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\">删除</a> ";
              }
              if (varButtonRight.indexOf('audit') >= 0) {
                  strRtn += "<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"audit\">审核</a> ";
              }
              return strRtn;
          }
      } 
    ]]
    , page: true
    , limit: 10
    , limits: [10, 15, 20, 25, 30]
    , text: {
        none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增 text: '对不起，加载出现异常！'
    }
    });

    //监听工具条
    table.on('tool(LAY-app-content-list)', function (obj) {
        //debugger;
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除吗？', function (index) {
                admin.req({
                    url: layui.setter.APIUrl + '/shop/order_returnMgr/BatchDelete' //实际使用请改成服务端真实接口
                  , data: { "ids": data.PID }
                  , type: "post"
                    //, success: function (res) {
                    //}
                  , done: function (res) {
                      layer.close(index); //执行关闭 
                      table.reload('LAY-app-content-list');
                      layer.msg('删除成功');
                  }
                });
            });
        }
        else if (obj.event === 'edit') {
            //location.hash = '/shop/shop_order_return/addChild/id=' + data.PID;
            admin.popup({
                title: '编辑信息'
                , area: ['850px', '650px']
              , id: 'LAY-popup-content-edit'
              , success: function (layero, index) {
                  view(this.id).render('/shop/shop_order_return/add', data).done(function () {
                      form.render(null, 'layuiadmin-app-form-list');

                      //监听提交
                      form.on('submit(layuiadmin-app-form-submit)', function (data) {
                          var field = data.field; //获取提交的字段 
                          //提交 Ajax 成功后，关闭当前弹层并重载表格 
                          admin.req({
                              url: layui.setter.APIUrl + '/shop/order_returnMgr/update' //实际使用请改成服务端真实接口
                          , data: field
                          , type: "post"
                          , done: function (res) {
                              layui.table.reload('LAY-app-content-list'); //重载表格
                              layer.close(index); //执行关闭
                              layer.msg('修改成功');
                          }
                          });
                      });
                  });
              }
            });
        }
        else if (obj.event === 'audit') {
            if (data.State != 0) {
                return layer.msg("该退款申请已审核或已取消。");
            }
            admin.popup({
                title: '退款审核'
                , area: ['62%', '85%']
              , id: 'LAY-popup-content-edit'
              , success: function (layero, index) {
                  view(this.id).render('/shop/shop_order_return/audit', data).done(function () {
                      form.render(null, 'layuiadmin-app-form-list');

                      //监听提交
                      form.on('submit(layuiadmin-app-form-submit)', function (data) {
                          var field = data.field; //获取提交的字段 
                          //提交 Ajax 成功后，关闭当前弹层并重载表格 
                          debugger;
                          var ReturnID = field.PID;
                          var State = field.AuditResult;
                          var Content = field.Content;
                          if (State == 2 && (Content == "" || Content == null)) {
                              return layer.msg("拒绝退款请输入审核原因");
                          }
                          var lindex = layer.load(1);
                          admin.req({
                              url: layui.setter.APIUrl + '/shop/order_returnMgr/ReturnAudit' //实际使用请改成服务端真实接口
                          , data: { "ReturnID": ReturnID, "State": State, "Content": Content }
                          , type: "post"
                          , async: false
                          , done: function (res) {
                              layui.table.reload('LAY-app-content-list'); //重载表格
                              layer.close(index); //执行关闭
                              layer.msg(res.ResultInfo);
                          }
                          });
                          layer.close(lindex);
                      });
                  });
              }
            });
        }
        else if (obj.event === 'retry') {
            layer.confirm('确定重新申请退款吗？', function (index) {
                admin.req({
                    url: layui.setter.APIUrl + '/shop/order_returnMgr/ReturnRetry' //实际使用请改成服务端真实接口
                  , data: { "ReturnID": data.PID }
                  , type: "post"
                  , done: function (res) {
                      layer.close(index); //执行关闭 
                      table.reload('LAY-app-content-list');
                      layer.msg(res.ResultInfo);
                  }
                });
            });
        }
        else if (obj.event === 'view') {
            admin.popup({
                title: '查看信息'
                , area: ['62%', '85%']
             , id: 'LAY-popup-content-view'
             , success: function (layero, index) {
                 view(this.id).render('/shop/shop_order_return/view', data).done(function () {
                     //监听提交
                     form.on('submit(layuiadmin-app-form-submit)', function (data) {
                         layer.close(index); //执行关闭
                     });
                 });
             }
            });
        }
    });

    exports('shop_order_return', {})

});
